<!--
  * 订单表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-26 06:40:44
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form" ref="formRef" :model="queryForm" :rules="rules">
    <a-row class="smart-query-form-row">
      <a-form-item label="交易订单号" name="id" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.id" placeholder="请输入聚合平台订单号" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OPERATION.ORDER" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ text, column }">
        <!-- 有图片预览时 注释解开并把下面的'picture'修改成自己的图片字段名即可 -->
        <!-- <template v-if="column.dataIndex === 'picture'">
                  <FilePreview :fileList="text" type="picture" />
                </template> -->

        <!-- 使用字典时 注释解开并把下面的'dict'修改成自己的字典字段名即可 有多个字典字段就复制多份同理修改 不然不显示字典 -->
        <!-- 方便修改tag的颜色 orange green purple success processing error default warning -->
        <!-- <template v-if="column.dataIndex === 'dict'">
                <a-tag color="cyan">
                  {{ text && text.length > 0 ? text.map((e) => e.valueName).join(',') : '暂无' }}
                </a-tag>
              </template> -->

        <template v-if="column.dataIndex === 'id'">
          <div style="display: flex; justify-content: space-between; align-items: center" @click="copy(text)">
            <div>{{ text }}</div>
            <component :is="$antIcons['CopyOutlined']" />
          </div>
        </template>

        <template v-if="column.dataIndex === 'payType'">
          <span>{{ $smartEnumPlugin.getDescByValue('ZZB_PAY_TYPE_ENUM', text) }}</span>
        </template>

        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate"></div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->
  </a-card>
</template>
<script setup>
  import { reactive, ref, getCurrentInstance } from 'vue'
  import { orderApi } from '/@/api/business/b-merchant/order/order-api'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import dayjs from 'dayjs'
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const.js'
  import { message } from 'ant-design-vue'
  import { formatToMoney, selectDictLabel } from '/@/utils/common'
  import { ZZB_PAY_WAY } from '/@/constants/dict'
  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '交易时间',
      dataIndex: 'fTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      width: 180,
      ellipsis: true,
    },
    {
      title: '交易订单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '门店简称',
      dataIndex: 'storeAbbrName',
      ellipsis: true,
    },
    {
      title: '收银员',
      dataIndex: 'storeStaffName',
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'channelName',
      ellipsis: true,
    },
    {
      title: '交易类型',
      dataIndex: 'payType',
      ellipsis: true,
    },
    {
      title: '支付方式',
      dataIndex: 'payWay',
      customRender: ({ text }) => selectDictLabel(ZZB_PAY_WAY, text),
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'tradeAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '实收金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '结算金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    // {
    //   title: '操作',
    //   dataIndex: 'action',
    //   fixed: 'right',
    //   width: 90,
    // },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------
  // 组件ref
  const formRef = ref()

  const queryFormState = {
    id: undefined, //订单号
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })

  const rules = {
    id: [{ required: true, message: '交易订单号 必填' }],
  }

  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])

  // 重置查询条件
  function resetQuery() {
    Object.assign(queryForm, queryFormState)
    tableData.value = []
  }

  // 搜索
  async function onSearch() {
    try {
      await formRef.value.validateFields()
      queryData()
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!')
    }
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await orderApi.findId(queryForm)
      tableData.value = [queryResult.data]
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }
</script>
